package cn.ccew.apk.web_manage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONSerializer;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.ccew.apk.model.Version;
import cn.ccew.sys.web.SysBaseController;

@Controller
public class VersionController extends SysBaseController {
	/**
	 * 加载左则树列表
	 * 
	 * @param response
	 * @param pid
	 * @throws Exception
	 */
	@RequestMapping("version_loadList.wb")
	public void version_loadList(String kindid, HttpServletRequest request, HttpServletResponse response) throws Exception {

		Criteria criteria = this.createCriteria(Version.class);

		if (org.apache.commons.lang.StringUtils.isNotEmpty(kindid) && !kindid.equals("-1")) {
			criteria.add(Restrictions.eq("softId", kindid));
		}

		criteria.addOrder(Order.desc("vstate"));
		criteria.addOrder(Order.desc("priority"));
		// 关联查询时使用Distinct方法去除重复数据
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		responseListJSON(criteria, request, response, new String[] {});
	}

	/**
	 * 加载一条记录，用于显示修改
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("version_load.wb")
	public void version_load(String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Version version = baseService.findById(id, Version.class);
		String listJson = JSONSerializer.toJSON(version).toString();
		writerTo(response, listJson);
	}

	/**
	 * 修改状态
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("version_modify_state.wb")
	public void version_modif_state(String id, int state, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Version version = baseService.findById(id, Version.class);
		try {
			// 只能启用一个
			if (state == 1) {
				List<Version> list = baseService.findListByProperty("softId", version.getSoftId(), Version.class);
				for (Version v : list) {
					v.setVstate(0);
					baseService.saveOrUpdate(v);
				}
			}

			version.setVstate(state);
			baseService.saveOrUpdate(version);
			writerSuccess(response, "保存成功");
		} catch (Exception e) {
			writerFailure(response, "保存失败，未知原因");
			e.printStackTrace();
		}
	}

	/**
	 * 保存
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("version_save.wb")
	public void version_save(@Valid Version version, String pid, HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			// 只能启用一个
			if (version.getStatus() == 1) {
				List<Version> list = baseService.findListByProperty("softId", version.getSoftId(), Version.class);
				for (Version v : list) {
					v.setVstate(0);
					baseService.saveOrUpdate(v);
				}
			}
			baseService.saveOrUpdate(version);
			writerSuccess(response, "保存成功");
		} catch (Exception e) {
			writerFailure(response, "保存失败，未知原因");
			e.printStackTrace();
		}

	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("version_delete.wb")
	public void version_delete(String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Version root = baseService.findById(id, Version.class);
		try {
			baseService.delete(root.getId(), Version.class);
			writerSuccess(response, "删除成功");
		} catch (Exception e) {
			writerFailure(response, "删除失败，未知原因");
			e.printStackTrace();
		}

	}

}
